﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace Bussines
{
    public class clCalendar
    {
        private SqlConnection sc;                
        public DataSet DS;
        SqlDataAdapter Adapter;
        TBLObj pBLObj = new TBLObj();

        public clCalendar(TBLObj iBLObj)
        {     
            pBLObj = iBLObj ;
            sc = iBLObj.sqlConnection;                    
        }

        public DataSet GetData(string iFilter)
        {
            string S = "select * from TERVEK";
            DS = new DataSet();
            Adapter = new SqlDataAdapter();
            if (iFilter!="")
            {
                S=S+" where "+iFilter;
            }
            S = S + " order by TRV_IDOPONT";
            Adapter.SelectCommand = new SqlCommand(S, sc);
            Adapter.Fill(DS, "TERVEK");
            return DS;
        }

        public DataSet GetData(int iID)
        {
            if (iID != -1)
            {
                return GetData("TRV_ID=" + iID.ToString());
            }
            else
            {
                return null;
            }
        }

        public DataSet GetData(DateTime iFrom, DateTime iTo, int iUser)
        {
            if (iFrom != null && iTo!=null)
            {
                return GetData("TRV_IDOPONT >= '" + iFrom.ToString("yyyy.MM.dd") + "' and TRV_IDOPONT < '" + iTo.ToString("yyyy.MM.dd") + "' and TRV_CREATED_BY="+iUser.ToString());
            }
            else
            {
                return null;
            }           
        }

        public bool UpdateTerv(string IID, string iName, string iLocation, DateTime iDate, string iComment, string iDone, ref string oErr)
        {
            SqlCommand cmd = new SqlCommand();

            oErr = "";
            cmd.Connection = sc;

            string S;

            S = "UPDATE TERVEK SET TRV_NEV='"+iName+"', TRV_HELYSZIN='"+iLocation+"', TRV_IDOPONT='"+iDate.ToString("yyyy.MM.dd hh:mm")+
                "', TRV_MEGJEGYZES='" + iComment + "', TRV_LETREJOTT='" + iDone + "', TRV_MODIFIED = GetDate() , TRV_MODIFIED_BY = " + pBLObj.USR_ID.ToString() + " where TRV_ID=" + IID;
                        
            cmd.CommandText = S;
            cmd.CommandType = CommandType.Text;
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {

                string s = "Hiba a terv mentése közben!";
                Utilities.LogError(pBLObj, e, "clCalendar", s);
                oErr = s;
                return false;
            }
        }

        public bool InsertTerv(string iName, string iLocation, DateTime iDate, string iComment, string iDone, ref string oErr)
        {
            SqlCommand cmd = new SqlCommand();

            oErr = "";
            cmd.Connection = sc;

            string S;

            S = "insert into TERVEK (TRV_NEV,TRV_HELYSZIN,TRV_IDOPONT,TRV_MEGJEGYZES,TRV_LETREJOTT,TRV_CREATED,TRV_CREATED_BY,TRV_MODIFIED,TRV_MODIFIED_BY) values ('" +
                iName + "','" + iLocation + "', '" + iDate.ToString("yyyy.MM.dd hh:mm") +
                "', '" + iComment + "','" + iDone + "',GetDate()," + pBLObj.USR_ID.ToString() + ",GetDate()," + pBLObj.USR_ID.ToString() + ")";

            cmd.CommandText = S;
            cmd.CommandType = CommandType.Text;
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {

                string s = "Hiba a terv mentése közben!";
                Utilities.LogError(pBLObj, e, "clCalendar", s);
                oErr = s;
                return false;
            }
        }

        public bool DeleteTerv(string iID, ref string oErr)
        {
            SqlCommand cmd = new SqlCommand();

            oErr = "";
            cmd.Connection = sc;

            string S;

            S = "delete from TERVEK where TRV_ID=" + iID;
            cmd.CommandText = S;
            cmd.CommandType = CommandType.Text;
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {

                string s = "Hiba a terv törlése közben!";
                Utilities.LogError(pBLObj, e, "clCalendar", s);
                oErr = s;
                return false;
            }
        }

        public int GeneratePartner(int iTRV_ID, ref string oErr)
        {
            int PRT_ID = -1;
            try
            {
                //get PRT_ID
                DataSet DS = new DataSet();
                SqlDataAdapter Adapter = new SqlDataAdapter();
                Adapter.SelectCommand = new SqlCommand("select IsNull(TRV_PRT_ID , -1) as PRT_ID from TERVEK WHERE TRV_ID = " + iTRV_ID.ToString(), pBLObj.sqlConnection);
                Adapter.Fill(DS, "TERVEK");

                PRT_ID = (int)DS.Tables["TERVEK"].Rows[0]["PRT_ID"];
                if (PRT_ID == -1)
                {
                    SqlCommand cmd = new SqlCommand();

                    //sc.Open();
                    cmd.Connection = sc;
                    cmd.CommandText = "SP_PARTNER_GEN";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@TRV_ID", iTRV_ID));
                    cmd.Parameters["@TRV_ID"].Direction = ParameterDirection.Input;
                    cmd.Parameters.Add(new SqlParameter("@PRT_ID", PRT_ID));
                    cmd.Parameters["@PRT_ID"].Direction = ParameterDirection.Output;
                    cmd.Parameters["@TRV_ID"].Value = iTRV_ID;
                    cmd.ExecuteNonQuery();
                    PRT_ID = Convert.ToInt32(Convert.ToInt32(cmd.Parameters["@PRT_ID"].Value.ToString()));
                    return PRT_ID;
                }
                else
                {
                    return PRT_ID;
                }
            }
            catch (Exception e)
            {

                string s = "Hiba a partner generálása közben!";
                Utilities.LogError(pBLObj, e, "clCalendar", s);
                oErr = s;
                return -1;
            }
        }

    }   
}
